package com.jinlejia.erp.ui.common;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.utils.DataFilter;
import com.weight.CustomToast;
import com.zhy.http.okhttp.OkHttpUtils;

import org.simple.eventbus.EventBus;

import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 添加标签
 */
public class AddTagsDialog extends AlertDialog {
    private Context mContext;
    public AddTagsDialog(Context context) {
        super(context);
        mContext = context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setCanceledOnTouchOutside(false);
        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
        window.setContentView(R.layout.dialog_add_tags);
        window.setGravity(Gravity.CENTER);
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        window.setBackgroundDrawable(new ColorDrawable());

        final EditText input = (EditText) window.findViewById(R.id.input_content);
        TextView cancel = (TextView) window.findViewById(R.id.id_cancel);
        TextView confirm = (TextView) window.findViewById(R.id.id_confirm);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if( !TextUtils.isEmpty( input.getText().toString() )) {
                    submitTag(input.getText().toString());
                }else{
                    CustomToast.showToast(mContext,"请输入内容",1500);
                }
            }
        });

    }
    private void submitTag( String tag ){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("tag",tag);
        OkHttpUtils.postString()
                .url(ApiConfig.TAGS_ADD)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(jsonObject.toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(mContext, response)) {
                            EventBus.getDefault().post("","TAG_ADD_TAG_SECCESS");
                        }
                    }
                });
    }
}
